<?php
    include('include/header.inc.php');  // Nous appelons notre header
    include('include/func.inc.php');
    $_SESSION['page'] = 'historique';
    $_SESSION['previous_page_detailTheme']= $_SERVER['REQUEST_URI'];
    
    if ($_SESSION['name'] == '')
        echo '<script>location.href="detailEvent.php?event='.$_SESSION['login'].'"</script>';
?>
<div data-role="content">	
    <script language='JavaScript'>

        //Variable to determine if all checkboxes must be checked
        var checkflag = "false";

        //Function to check or uncheck all checkboxes
        function check(field, id_event) {
            var nom = 'checkbox' + id_event+"-0";
            var tmpNom, tmpNom2 ='checkbox' + id_event+"-";
            var check = false;
            for (var i=0; i<field.checkbox.length; i++){
                tmpNom =field.checkbox[i].id;
                
                if(tmpNom.indexOf(nom)==0){
                    check = field.checkbox[i].checked;
                }
                if(tmpNom.indexOf(tmpNom2)==0){
                    if (check){
                        field.checkbox[i].checked = true;
                    }
                    else{
                        field.checkbox[i].checked = false;
                    }
                }
            }    
        }


        //To define which themes are checked to subscribe
        function selectedThemes(field){
            var nom, theme='';
            for (var i=0; i<field.checkbox.length; i++){
                if(field.checkbox[i].checked){
                    nom =field.checkbox[i].id;
                    nom = nom.substr(nom.indexOf('-')+1,nom.length-1);
                    if (nom!='0'){
                        theme += nom + '-';
                    }
                }
            } 

            //If no theme checked
            if(theme == ''){
                displayNoThemeSeleted();
            }
            else{//One or more themes checked
                theme = theme.substr(0, theme.length-1);
                //Verify if there are some existing registration for some themes in the event
                location.href = "./unregisterMultiple.php?theme=" + theme;
            }
        }

        //Function to display that a theme must be selected
        function displayNoThemeSeleted(){
            document.getElementById("noThemeSeleted").innerHTML = "<?php echo $texts['noThemeSelected'][$_SESSION['lang']]?>";
        }
    </script>
    <?php    
                $username = strtoupper($_SESSION['login']);
                
		echo'<H2>'.$_SESSION['name'].'</h2>';
                ?>
            <div class="tableauPage">
                <?php
	
                $rfc = connectSAP();					
			
                //We must know if the function really exists
                $fce = saprfc_function_discover($rfc, "Z_FM_M646_GET_HISTORY");
                if(!$fce){
                        echo "The function module has failed.";
                    echo $rfc;
                    exit;
                }    
                
                //Pass import parameters
                saprfc_import ($fce,"IV_USERNAME",$username);

                //Pass table parameters
                saprfc_table_init ($fce,"GET_REGISTRATIONS");

                //Call and execute the function
                $rc = saprfc_call_and_receive ($fce);            
                if ($rfc_rc != SAPRFC_OK){
                        if ($rfc == SAPRFC_EXCEPTION ){
                        echo ("Exception raised: ".saprfc_exception($fce));
                        } else {
                    echo ("Call error: ".saprfc_error($fce));
                        }
                        exit;
                    }

                //Fetch the data from the internal tables
                $data_row = saprfc_table_rows ($fce,"GET_REGISTRATIONS");
              
                $status = array();

                for ($i=1; $i<=$data_row; $i++)
                {
                    $DATARow = saprfc_table_read ($fce,"GET_REGISTRATIONS",$i);
                    $idTheme[$i-1] = $DATARow['ID_THEME'];
                    $status[$i-1][0] = $DATARow['ID_THEME'];
                    $status[$i-1][1] = $DATARow['STATUS'];
                }    

                //realease the function and close the connection
                saprfc_function_free($fce);
                saprfc_close($rfc);
                
                //To compare the registration dates with the current date
                $today = date('d.m.Y');
                $todayFormat = explode(".", $today); 
                $dateToday = $todayFormat[2].$todayFormat[1].$todayFormat[0];
            
                for ($i=0; $i<count($idTheme); $i++)
                {
                    //Try to connect to SAP using our Login array
                    $rfc = connectSAP();
                    if(!$rfc){
                            echo "The RFC connection has failed with the following error:".saprfc_error();
                        exit;
                    }			

                    //We must know if the function really exists
                    $fce = saprfc_function_discover($rfc, "Z_FM_M646_GET_THEMES");
                    if(!$fce){
                        echo "The function module has failed.";
                        echo $rfc;
                        exit;
                    }    
                    //Pass import parameters
                    saprfc_import ($fce,"IV_THEME",$idTheme[$i]);

                    //Pass table parameters
                    saprfc_table_init ($fce,"TAB_DETAILS_THEME");

                    //Call and execute the function
                    $rc = saprfc_call_and_receive ($fce);            
                    if ($rc != SAPRFC_OK){
                            if ($rfc == SAPRFC_EXCEPTION ){
                            echo ("Exception raised: ".saprfc_exception($fce));
                            } else {
                        echo ("Call error: ".saprfc_error($fce));
                            }
                            exit;
                        }

                    //Fetch the data from the internal tables
                    $data_row = saprfc_table_rows ($fce,"TAB_DETAILS_THEME");
                    $DATARow = saprfc_table_read ($fce,"TAB_DETAILS_THEME",1);
                    $data[] = array('id_Event' => $DATARow['REGISTRATION_ID'],
                                    'Event' => $DATARow['EVENT'],
                                    'id_Theme' => $idTheme[$i],
                                    'Theme' => $DATARow['TITLE_THEME'],
                                    'StartDate' => $DATARow['STARTDATE'],
                                    'EndDate' => $DATARow['ENDDATE']);
                    //realease the function and close the connection
                    saprfc_function_free($fce);
                    saprfc_close($rfc);
                 }
                 if ($data_row > 0)
                 {
                    foreach ($data as $key => $row) {
                        $event_id[$key] = $row['id_Event'];
                        $event[$key]  = $row['Event'];
                        $theme_id[$key] = $row['id_Theme'];
                        $theme[$key] = $row['Theme'];
                        $startDate[$key] = $row['StartDate'];
                        $endDate[$key] = $row['EndDate'];
                    }
                    array_multisort($endDate, SORT_DESC, $startDate, SORT_ASC, $data);
                    
                    
                    $nbEvent = 0;
                    $useEvent = 0;
                    $dateEvent = 0;
                    echo'<form id="tabForm" name="tabForm">';
                    echo '<table width="100%" cellspacing="0">';
                    foreach ($data as $row)
                    {
                        $dateold = $dateEvent;
                        
                        $startdate = substr($row['StartDate'], 6, 2) . "." . substr($row['StartDate'], 4, 2) . "." . substr($row['StartDate'], 0, 4);
                        $enddate = substr($row['EndDate'], 6, 2) . "." . substr($row['EndDate'], 4, 2) . "." . substr($row['EndDate'], 0, 4);
                        $todayFormat = explode(".", $startdate); 
                        $dateEvent = $todayFormat[2].$todayFormat[1].$todayFormat[0];
                        if ($useEvent <>$row['id_Event'])
                        {
                            if ($nbEvent > 0){
                                /*echo'</table>';
                                if ($dateToday < $dateold)
                                    echo'<input type="button" value="' . $texts['unregister_theme'][$_SESSION['lang']] . '" onClick="selectedThemes('.$eventold.','.$themeold.', this.form)" />';
                               echo'</form>';*/
                            }
                            //echo'<form id="tabForm'.$row['id_Event'].'" name="tabForm'.$row['id_Event'].'">';
                            //echo '<table width="100%" cellspacing="0">';
                            echo '<tr class="titreTableau">';
                                echo'<td colspan="2" width="80%">'.htmlentities($row['Event']).'</td>';
                                echo'<td align="center" width="10%">'.$texts['status'][$_SESSION['lang']].'</td>';
                                if ($dateToday < $dateEvent)
                                    echo '<td align="center" width="10%"><input name="checkbox" type="checkbox" id="checkbox'.$row['id_Event'].'-0" onClick="this.value=check(this.form,'.$row['id_Event'].');"/></td>';
                                else
                                    echo '<td align="center" width="10%"><input name="checkbox" DISABLED type="checkbox" id="checkbox'.$row['id_Event'].'-0" onClick="this.value=check(this.form,'.$row['id_Event'].');"/></td>';
                            echo '</tr>';
                            $useEvent = $row['id_Event'];
                        }
                        echo '<tr>';
                            echo'<td width=\"60%\"><a href="./detailTheme.php?event='.$useEvent.'&theme='.$row['id_Theme'].'" rel="external">'.htmlentities($row['Theme']).'</a></td>';

                            if ($startdate == $enddate)
                                echo'<td align="right" width=\"20%\">'.$enddate.'</td>';
                            else                            
                                echo'<td align="right" width=\"20%\">'.$startdate.' - '.$enddate.'</td>';
                            echo "<td align=\"center\" width=\"10%\">";
                            for ($y=0; $y<=sizeof($status); $y++){
                                if($status[$y][0] == $row['id_Theme']){
                                    if($status[$y][1] == 'r'){
                                        echo "<img src='./picture/green.gif'/>";
                                    }
                                    else if($status[$y][1] == 'w'){
                                        echo "<img src='./picture/orange.gif'/>";
                                    }
                                }
                            }
                            echo "</td>";
//Ajouter la légende                            
//echo '<td align="center" width="10%"></td>';
                            if ($dateToday < $dateEvent)
                                echo "<td align=\"center\" width=\"10%\"><input type=\"checkbox\" id=\"checkbox".$row['id_Event']."-".$row['id_Theme']."\" name=\"checkbox\"/></td>";
                            else
                                echo "<td align=\"center\" width=\"10%\"><input type=\"checkbox\" DISABLED id=\"checkbox".$row['id_Event']."-".$row['id_Theme']."\" name=\"checkbox\"/></td>";
                        echo '</tr>';
                        $nbEvent ++;
                        $eventold = $row['id_Event'];
                        $themeold = $row['id_Theme'];
                    }
                    if ($nbEvent > 0){
                        echo'</table>';
                        echo "<br/>*<img src='./picture/orange.gif'/> =  " . $texts['waiting_status'][$_SESSION['lang']] . ", <img src='./picture/green.gif'/> = " . $texts['registered_status'][$_SESSION['lang']] . "</br>";
                        //Paragaphe will be display if no theme selected when subscription button clicked    
                        echo'<p id="noThemeSeleted" align="center" style="color:red; font-weight:bold;"></p>';
                        echo'<p align="center"><input type="button" data-inline="true" value="' . $texts['unregister_theme'][$_SESSION['lang']] . '" onClick="selectedThemes(this.form)" />';
                        echo '<a href="./index.php" data-role="button" data-inline="true">'.$texts['home'][$_SESSION['lang']].'</a></p>';
                        echo'</form>';
                    }
                     
                 }
                 else
                 {
                     echo $texts['emptyHistory'][$_SESSION['lang']];
                 }
                ?>
            </div>
	</div><!-- /content -->
    <?php
        include('include/footer.inc.php');  // Nous appelons notre menu
    ?>

</body>
</html>